﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using LiteExpress.Utilities.CryptoExtension;

namespace LiteExpress.Utilities
{
    public class CryptoHelper
    {
        /// <summary>
        /// 获得输入字符串的md5签名，去除“-”，并转为小写格式
        /// </summary>
        /// <param name="inputstr"></param>
        /// <returns></returns>
        public static string GetMd5Sign(string inputstr, Encoding encode = null)
        {
            if (encode == null)
            {
                encode = Encoding.UTF8;
            }

            byte[] data = encode.GetBytes(inputstr);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string resultCode = BitConverter.ToString(result).Replace("-", "").ToLower();
            return resultCode;
        }


        /// <summary>
        /// use sha1 to encrypt string
        /// </summary>
        public static string GetSHA1Sign(string inputstr, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] StrRes = encoding.GetBytes(inputstr);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            string result = BitConverter.ToString(StrRes).Replace("-", "").ToLower();
            return result;
        }

        /// <summary>
        /// des加密
        /// </summary>        
        /// <param name="raw">原始字符串</param>
        /// <param name="txtCoding">只能为hex或base64</param>
        /// <param name="coding">字符编码，默认utf8</param>
        public static string DESEncrypt(byte[] IV, byte[] Key, string raw, string txtCoding = "base64", Encoding coding = null)
        {
            if (coding == null)
            {
                coding = Encoding.UTF8;
            }

            Byte[] StrBytes = coding.GetBytes(raw);

            DESCryptoServiceProvider dcp = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, dcp.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cs.Write(StrBytes, 0, StrBytes.Length);
            cs.FlushFinalBlock();

            if (txtCoding == "hex")
            {
                return ms.ToArray().ToHex();
            }
            return ms.ToArray().ToBase64();
        }

        /// <summary>
        /// des解密
        /// </summary>
        /// <param name="encrypted">加密后的字符串</param>
        /// <param name="txtCoding">只能为hex或base64</param>
        /// <param name="coding">字符编码，默认utf8</param>
        public static string DESDecrypt(byte[] IV, byte[] Key, string encrypted, string txtCoding = "base64", Encoding coding = null)
        {
            if (coding == null)
            {
                coding = Encoding.UTF8;
            }

            DESCryptoServiceProvider dcp = new DESCryptoServiceProvider();

            MemoryStream ms;
            if (txtCoding == "hex")
            {
                ms = new MemoryStream(encrypted.HexToBytes());
            }
            else
            {
                ms = new MemoryStream(encrypted.Base64ToBytes());
            }
            CryptoStream cs = new CryptoStream(ms, dcp.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs, coding);
            return sr.ReadToEnd();
        }


        /// <summary>
        /// aes加密
        /// </summary>        
        /// <param name="raw">原始字符串</param>
        /// <param name="txtCoding">只能为hex或base64</param>
        /// <param name="coding">字符编码，默认utf8</param>
        public static string AESEncrypt(byte[] IV, byte[] Key, string raw, string txtCoding = "base64", Encoding coding = null)
        {
            if (coding == null)
            {
                coding = Encoding.UTF8;
            }

            Byte[] StrBytes = coding.GetBytes(raw);

            var dcp = new AesCryptoServiceProvider();
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, dcp.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cs.Write(StrBytes, 0, StrBytes.Length);
            cs.FlushFinalBlock();

            if (txtCoding == "hex")
            {
                return ms.ToArray().ToHex();
            }
            return ms.ToArray().ToBase64();
        }

        /// <summary>
        /// aes解密
        /// </summary>
        /// <param name="encrypted">加密后的字符串</param>
        /// <param name="txtCoding">只能为hex或base64</param>
        /// <param name="coding">字符编码，默认utf8</param>
        public static string AESDecrypt(byte[] IV, byte[] Key, string encrypted, string txtCoding = "base64", Encoding coding = null)
        {
            if (coding == null)
            {
                coding = Encoding.UTF8;
            }

            var dcp = new AesCryptoServiceProvider();

            MemoryStream ms;
            if (txtCoding == "hex")
            {
                ms = new MemoryStream(encrypted.HexToBytes());
            }
            else
            {
                ms = new MemoryStream(encrypted.Base64ToBytes());
            }
            CryptoStream cs = new CryptoStream(ms, dcp.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs, coding);
            return sr.ReadToEnd();
        }
    }
}
